package com.mrart.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mrart.entity.Role;
import com.mrart.mysql.mapper.RoleMapper;
import com.mrart.service.ResourceService;
import com.mrart.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private ResourceService resourceService;
    
    /* (non-Javadoc)
     * @see com.mrart.service.impl.RoleService#findAll()
     */
    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }
    
    /* (non-Javadoc)
     * @see com.mrart.service.impl.RoleService#selectByPrimaryKey(java.lang.Long)
     */
    @Override
    public Role selectByPrimaryKey(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public Set<String> findRoles(Long... roleIds) {
        Set<String> roles = new HashSet<String>();
        for(Long roleId : roleIds) {
            Role role = selectByPrimaryKey(roleId);
            if(role != null) {
                roles.add(role.getRole());
            }
        }
        return roles;
    }
    
    @Override
    public Set<String> findPermissions(Long[] roleIds) {
        Set<Long> resourceIds = new HashSet<Long>();
        for(Long roleId : roleIds) {
            Role role = selectByPrimaryKey(roleId);
            if(role != null) {
                resourceIds.addAll(role.getResourceIdList());
            }
        }
        return resourceService.findPermissions(resourceIds);
    }
}
